package com.air.service;

import com.air.vo.query.BlogSearchCondition;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.air.domain.Blog;
import com.air.mapper.BlogMapper;

import java.util.Date;

/**
 * @author shent
 */
@Service
public class BlogService extends ServiceImpl<BlogMapper, Blog> {
    @Autowired
    BlogMapper blogMapper;

    /**
     * 新增博客
     *
     * @param blog 博客信息
     * @return 主键id
     */
    public long addBlog(Blog blog) {
        blog.setCreateTime(new Date());
        blog.setAuthor("shent");
        blogMapper.insert(blog);
        return blog.getId();
    }

    /**
     * 条件分页查询博客
     *
     * @param condition 查询条件
     * @return 博客List
     */
    public IPage<Blog> searchBlog(BlogSearchCondition condition) {
        QueryWrapper<Blog> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(condition.getTitle())) {
            wrapper.eq("title", condition.getTitle());
        }
        if (!StringUtils.isEmpty(condition.getType())) {
            wrapper.eq("type", condition.getType());
        }
        if (!StringUtils.isEmpty(condition.getLabel())) {
            wrapper.eq("label", condition.getLabel());
        }
        if (!StringUtils.isEmpty(condition.getRecommendLevel())) {
            wrapper.eq("recommend_level", condition.getRecommendLevel());
        }
        if (!StringUtils.isEmpty(condition.getStatus())) {
            wrapper.eq("status", condition.getStatus());
        }
        if (!StringUtils.isEmpty(condition.getIsCreate())) {
            wrapper.eq("is_create", condition.getIsCreate());
        }
        Page<Blog> page = new Page<>(condition.getPageNumber(), condition.getPageSize());
        IPage<Blog> pageList = this.page(page, wrapper);
        return pageList;
    }

    public long editBlog(Blog blog) {
        blog.setUpdateTime(new Date());
        return blogMapper.updateById(blog);
    }
}
